﻿using System.Collections.Generic;
using System.IO;
using System;

namespace ShpMergerConsole
{
    internal class Performer
    {
        private string command;
        private string parameter;
        private List<string> srcFiles;
        private string destFile;

        public static bool Quit { set; get; }
        public static bool Merging { set; get; }

        private bool PickCommand(string order)
        {
            bool res = true;
            if (order.Length == 2)
            {
                command = order;
                parameter = "";
            }
            else
            {
                int intervalIndex = order.IndexOf(' ');
                if (intervalIndex != 3)
                {
                    command = "";
                    parameter = "";
                    res = false;
                }
                else
                {
                    command = order.Substring(0, intervalIndex);
                    parameter = order.Substring(intervalIndex + 1);
                }
            }
            return res;
        }

        private bool PickFilesByDirectory()
        {
            srcFiles.Clear();
            bool res = false;
            string[] filePaths = parameter.Split(' ');
            if (filePaths.Length != 2)
            {
                Console.Write(OutputString.WrongParameter);
            }
            else if (!Directory.Exists(filePaths[0]))
            {
                Console.Write(OutputString.WrongSrcDirectory);
            }
            else
            {
                try
                {
                    foreach (string file in Directory.GetFiles(filePaths[0]))
                    {
                        string xx = Path.GetExtension(file).ToLower();
                        if (Path.GetExtension(file).ToLower() == ".shp")
                        {
                            srcFiles.Add(file);
                        }
                    }
                    if (srcFiles.Count < 2)
                    {
                        Console.Write(OutputString.LackSrcFiles);
                    }
                    else if (!Directory.Exists(Path.GetDirectoryName(filePaths[1])))
                    {
                        Console.Write(OutputString.WrongDestDirectory);
                    }
                    else if (Path.GetExtension(filePaths[1]).ToLower() != ".shp")
                    {
                        Console.Write(OutputString.WrongDestFile);
                    }
                    else
                    {
                        destFile = filePaths[1];
                        res = true;
                    }
                }catch(Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
            }
            return res;
        }

        private bool PickFilesByFiles()
        {
            srcFiles.Clear();
            bool res = false;
            string[] filePaths = parameter.Split(' ');
            if (filePaths.Length < 3)
            {
                Console.Write(OutputString.WrongParameter);
            }
            else
            {
                string wrongFile = "";
                string repetitiveFile = "";
                int k;
                try{
                    for (k = 0; k < filePaths.Length - 1; k++)
                    {
                    
                        if (!File.Exists(filePaths[k]) 
                            && Path.GetExtension(filePaths[k]).ToLower() != ".shp")
                        {
                            wrongFile += "[" + (k + 1).ToString() + "] ";       
                        }
                        else if (srcFiles.Contains(filePaths[k]))
                        {
                            repetitiveFile += "[" 
                                + srcFiles.IndexOf(filePaths[k]).ToString() 
                                + "][" + (k + 1).ToString() + "] ";
                        }
                        srcFiles.Add(filePaths[k]);
                   
                    }
                    if (wrongFile != "")
                    {
                        Console.Write(OutputString.WrongSrcFiles(wrongFile));
                    }
                    else if(repetitiveFile != "")
                    {
                        Console.Write(OutputString.RepetitiveFiles(repetitiveFile));
                    }
                    else if (!Directory.Exists(Path.GetDirectoryName(filePaths[k])))
                    {
                        Console.Write(OutputString.WrongDestDirectory);
                    }
                    else if (Path.GetExtension(filePaths[k]).ToLower() != ".shp")
                    {
                        Console.Write(OutputString.WrongDestFile);
                    }
                    else
                    {
                        destFile = filePaths[k];
                        res = true;
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return false;
                }
                
            } 
            return res;
        }

        public Performer()
        {
            srcFiles = new List<string>();
            Performer.Merging = false;
            Performer.Quit = false;
        }

        public void Reponse(string order)
        {
            if (PickCommand(order) == false)
            {
                Console.Write(OutputString.WrongOrder);
            }
            else if (command == "-v")
            {
                Console.Write(OutputString.Version);
            }
            else if (command == "-h")
            {
                Console.Write(OutputString.Help);
            }
            else if (command == "-c")
            {
                Console.Clear();
                Console.Write(OutputString.Version);
            }
            else if (command == "-mf" && PickFilesByFiles())
            {
                new FileMeger().Merge(srcFiles, destFile);
            }
            else if (command == "-md" && PickFilesByDirectory())
            {
                new FileMeger().Merge(srcFiles, destFile);
            }
            else if (command == "-q")
            {
                Performer.Quit = true;
            }
        }
    }
}
